package com.mayihr.fx.attributeBind.tableViewBind;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.CheckBoxTreeCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class bindTest extends Application implements Initializable {
    @FXML
    private ChoiceBox<Account> choice;

    @FXML
    private TableView<Account> table;

    @FXML
    private TreeView<Account> tree;
    //    private Account[] accounts;
    private List<Account> accounts;

//    {
//        accounts = new Account[]{
//                new Account("Tom1", 1),
//                new Account("tom2", 2),
//                new Account("tom3", 3),
//                new Account("tom4", 4),
//                new Account("tom5", 5)
//        };
//    }

    {
        accounts = new ArrayList<>();
        accounts.add(new Account("Tom1", 1));
        accounts.add(new Account("Tom2", 2));
        accounts.add(new Account("Tom3", 3));
        accounts.add(new Account("Tom4", 4));
        accounts.add(new Account("Tom5", 5));
//        accounts.add(new Account2("Tom1", "1"));
//        accounts.add(new Account2("Tom2", "2"));
//        accounts.add(new Account2("Tom3", "3"));
//        accounts.add(new Account2("Tom4", "4"));
//        accounts.add(new Account2("Tom5", "5"));

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList accountss =FXCollections.observableArrayList(accounts);
//        table.setEditable(true);


        TreeItem root = new TreeItem("project");
        root.getChildren().addAll(accounts);

        tree.setRoot(root);
        tree.setCellFactory(CheckBoxTreeCell.forTreeView());
        for (Account account : accounts) {
            TreeItem<Account> userItem = new TreeItem(account.getName());
            tree.getRoot().getChildren().add(userItem);
        }



        TableColumn<Account,String>  nameCol = new TableColumn("Name");
        TableColumn<Account,String>  ageCol = new TableColumn("Age");
        TableColumn  checkCol = new TableColumn("Checkbox");
        TableColumn actCol = new TableColumn("Action");

        nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        ageCol.setCellValueFactory(new PropertyValueFactory<>("age"));
//        checkCol.setCellFactory(new Callback<TableColumn, TableCell>() {
//            @Override
//            public TableCell call(TableColumn param) {
//                return new CheckBoxTableCell();
//            }
//        });

        table.getColumns().addAll(nameCol, ageCol,checkCol,actCol);
        table.setItems(accountss);


        choice.getItems().addAll(accounts);
        choice.setValue(accounts.get(0));
        choice.converterProperty().set(new StringConverter<Account>() {
            @Override
            public String toString(Account object) {
                return object.getName();
            }

            @Override
            public Account fromString(String string) {
                return null;
            }
        });

        choice.valueProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("aaa");
            table.getItems().add(choice.getValue());
            Account account = choice.getValue();
            System.out.println(account);
            System.out.println(observable + "," + oldValue + "," + newValue);
            System.out.println(observable.getValue());
        });
        choice.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("bbb");
            Account account = choice.getValue();
            System.out.println(account);
            System.out.println(observable + "," + oldValue + "," + newValue);
            System.out.println(observable.getValue());
        });
    }


    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("./bindTest.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setTitle("FXML Welcome");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
